package org.liquidByte.simpleChat.guiClient;

import java.net.UnknownServiceException;

import org.liquidByte.simpleChat.communication.RemoteCallHandler;
import org.liquidByte.simpleChat.dto.User;
import org.liquidByte.simpleChat.dto.UserIdentification;
import org.liquidByte.simpleChat.dto.UserMessage;

public class RemoteCallHandlerImpl implements RemoteCallHandler {
	private EventService eventService;

	public EventService getEventService() {
		if (this.eventService == null) {
			this.eventService = new EventService();
		}
		return eventService;
	}
	
	@Override
	public Object handleCall(Object operation, Object[] params) throws Exception {
		String strOperation = (String)operation;
		
		if (strOperation.equals("event/onUserOnline")) {
			this.getEventService().onUserOnline((User)params[0]);
			return null;
		}
		else if (strOperation.equals("event/onUserOffline")) {
			this.getEventService().onUserOffline((UserIdentification)params[0]);
			return null;
		}
		
		else if (strOperation.equals("event/onMessageReceived")) {
			this.getEventService().onMessageReceived((UserMessage)params[0]);
			return null;
		}
		else if (strOperation.equals("event/setStatus")) {
			this.getEventService().onStatusChanged((User)params[0]);
			return null;
		}
		else {
			throw new UnknownServiceException("no such service for operation " + strOperation);
		}
	}

}
